package cn.autumnorange.controller.impl;

import cn.autumnorange.controller.OrderController;
import cn.autumnorange.dto.CreateOrderDTO;
import cn.autumnorange.dto.EditOrderDTO;
import cn.autumnorange.enums.ResultEnum;
import cn.autumnorange.exception.ChanelException;
import cn.autumnorange.service.OrderService;
import cn.autumnorange.util.ResultUtil;
import cn.autumnorange.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName OrderControllerImpl
 * @Description
 * @Date 2019/4/11 15:38
 * @Version 1.0
 */
@RestController
@Slf4j
public class OrderControllerImpl implements OrderController {

    @Autowired
    private OrderService orderService;

    @Override
    public ResultVO createOrder(@RequestBody CreateOrderDTO createOrderDTO) {
        if (StringUtils.isEmpty(createOrderDTO.getBuyerAddress()) || StringUtils.isEmpty(createOrderDTO.getBuyerName()) || StringUtils.isEmpty(createOrderDTO.getBuyerPhone())){
            log.error("【创建订单】参数不能为空, createOrderDTO={}", createOrderDTO);
            throw new ChanelException(ResultEnum.PARAM_ERROR);
        }
        createOrderDTO = orderService.createOrder(createOrderDTO);
        return ResultUtil.success(createOrderDTO);
    }

    @Override
    public ResultVO editOrder(@RequestBody EditOrderDTO editOrderDTO) {
        if (StringUtils.isEmpty(editOrderDTO.getBuyerAddress()) || StringUtils.isEmpty(editOrderDTO.getBuyerName()) || StringUtils.isEmpty(editOrderDTO.getBuyerPhone())){
            log.error("【创建订单】参数不能为空, editOrderDTO={}", editOrderDTO);
            throw new ChanelException(ResultEnum.PARAM_ERROR);
        }
        editOrderDTO = orderService.editOrder(editOrderDTO);
        return ResultUtil.success(editOrderDTO);
    }

    @Override
    public ResultVO deleteOrder(@RequestParam("orderId") String orderId) {
        if (StringUtils.isEmpty(orderId)){
            log.error("【创建订单】参数不能为空, orderId={}", orderId);
            throw new ChanelException(ResultEnum.PARAM_ERROR);
        }
        return ResultUtil.success(orderService.deleteOrder(orderId));
    }

    @Override
    public ResultVO auditOrder(@RequestParam("orderId") String orderId, @RequestParam("orderStatus") int orderStatus) {
        if (StringUtils.isEmpty(orderId) || StringUtils.isEmpty(orderStatus)){
            log.error("【创建订单】参数不能为空, orderId={}, orderStatus={}", orderId, orderStatus);
            throw new ChanelException(ResultEnum.PARAM_ERROR);
        }
        return ResultUtil.success(orderService.auditOrder(orderId, orderStatus));
    }

    @Override
    public ResultVO deliverGoods(@RequestParam("orderId") String orderId, @RequestParam("deliveryStatus") int deliveryStatus) {
        if (StringUtils.isEmpty(orderId) || StringUtils.isEmpty(deliveryStatus)){
            log.error("【创建订单】参数不能为空, orderId={}, deliveryStatus={}", orderId, deliveryStatus);
            throw new ChanelException(ResultEnum.PARAM_ERROR);
        }
        return ResultUtil.success(orderService.deliverGoods(orderId, deliveryStatus));
    }

    @Override
    public ResultVO courierNumber(@RequestParam("orderId") String orderId, @RequestParam("courierNumber") String courierNumber) {
        if (StringUtils.isEmpty(orderId) || StringUtils.isEmpty(courierNumber)){
            log.error("【创建订单】参数不能为空, orderId={}, courierNumber={}", orderId, courierNumber);
            throw new ChanelException(ResultEnum.PARAM_ERROR);
        }
        return ResultUtil.success(orderService.courierNumber(orderId, courierNumber));
    }

    @Override
    public ResultVO orderdetail(@RequestParam("orderId") String orderId) {
        if (StringUtils.isEmpty(orderId)){
            log.error("【创建订单】参数不能为空, orderId={}", orderId);
            throw new ChanelException(ResultEnum.PARAM_ERROR);
        }
        return ResultUtil.success(orderService.orderdetail(orderId));
    }

    @Override
    public ResultVO orderList(@RequestParam(value = "page", defaultValue = "0") Integer page,
                              @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(page, size, sort);
        return ResultUtil.success(orderService.orderList(pageRequest).getContent());
    }

    @Override
    public ResultVO findOrder(@RequestParam("buyerPhone") String buyerPhone,
                              @RequestParam(value = "page", defaultValue = "0") Integer page,
                              @RequestParam(value = "size", defaultValue = "10") Integer size) {
        // 如果buyerPhone为空，返回订单列表
        if (StringUtils.isEmpty(buyerPhone)){
            Sort sort = new Sort(Sort.Direction.DESC, "createTime");
            PageRequest pageRequest = new PageRequest(page, size, sort);
            return ResultUtil.success(orderService.orderList(pageRequest).getContent());
        }
        return ResultUtil.success(orderService.findOrder(buyerPhone, new PageRequest(page, size)).getContent());
    }

    @Override
    public ResultVO auditOrderList(@RequestParam("orderStatus") int orderStatus,
                                   @RequestParam(value = "page", defaultValue = "0") Integer page,
                                   @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(orderStatus)){
            log.error("【创建订单】订单状态不能为空, orderStatus={}", orderStatus);
            throw new ChanelException(ResultEnum.PARAM_ERROR);
        }
        return ResultUtil.success(orderService.auditOrderList(orderStatus, new PageRequest(page, size)).getContent());
    }

    @Override
    public ResultVO deliveryOrderList(@RequestParam("deliveryStatus") int deliveryStatus,
                                      @RequestParam("orderStatus") int orderStatus,
                                      @RequestParam(value = "page", defaultValue = "0") Integer page,
                                      @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(deliveryStatus)){
            log.error("【创建订单】发货状态不能为空, deliveryStatus={}", deliveryStatus);
            throw new ChanelException(ResultEnum.PARAM_ERROR);
        }
        return ResultUtil.success(orderService.deliveryOrderList(deliveryStatus, orderStatus, new PageRequest(page, size)).getContent());
    }
}
